using System.Threading;
using System.Threading.Tasks;
using MediatR;
using B.S.BasicData.EroorCode;
using B.S.BasicData.Domain;
using B.S.BasicData.Infrature;

public class ProindexinfoAddHandler : IRequestHandler<ProindexinfoAddRequest, ApiResult<Proindexinfo>>
{
    private readonly IBaseRepository<Proindexinfo> _repository;
    public ProindexinfoAddHandler(IBaseRepository<Proindexinfo> repository)
    {
        _repository = repository;
    }

    public async Task<ApiResult<Proindexinfo>> Handle(ProindexinfoAddRequest request, CancellationToken cancellationToken)
    {
        var entity = new Proindexinfo
        {
            ProductNameId = request.ProductNameId,
            BatchNo = request.BatchNo,
            ShelfLife = request.ShelfLife,
            LicenseNo = request.LicenseNo,
            Manufacturer = request.Manufacturer,
            CoverImage = request.CoverImage,
            InfoCompleteness = request.InfoCompleteness,
            IsDeleted = request.IsDeleted,
            ModifiedAt = request.ModifiedAt,
            CreatedAt = request.CreatedAt,
            CreatedBy = request.CreatedBy,
            ModifiedBy = request.ModifiedBy,
        };
        try
        {
            await _repository.AddAsync(entity);
            return ApiResult<Proindexinfo>.Success(entity, "添加成功");
        }
        catch (System.Exception ex)
        {
            return ApiResult<Proindexinfo>.Fail($"添加失败: {ex.Message}");
        }
    }
} 